
package services;

import dao.KeywordJpaController;
import dao.exceptions.NonexistentEntityException;
import entities.Keyword;
import entities.Problem;
import exceptions.DatabaseException;
import exceptions.NonExistentObjectException;
import exceptions.PreexistingObjectException;
import java.util.ArrayList;
import java.util.List;
import vo.VOKeyword;

/**
 *
 * @author sbaldrich
 */
public class KeywordService {

    private static KeywordJpaController kc;
    public static KeywordJpaController getInstance()
    {
        if(kc == null)
            kc = new KeywordJpaController();
        return kc;
    }

    private static ProblemService ps;
    public static ProblemService getProblemService()
    {
        if(ps == null)
            ps = new ProblemService();
        return ps;
    }


    public void save(VOKeyword vo) throws Exception
    {
        kc = getInstance();
        Keyword k = findObject(vo.getName());
        if(k!=null)
            throw new PreexistingObjectException("Keyword");
        try
        {
            kc.create(k);
        } catch (Exception e)
        {
            throw new DatabaseException("KeywordService.save");
        }
    }

    public VOKeyword find(String name)throws Exception
    {
        kc = getInstance();
        Keyword k = findObject(name);
        if(k==null)
            return null;
        return k.getVO();
    }
    
    public Keyword findObject(String name)throws Exception
    {
        kc = getInstance();
        try
        {
            return kc.findKeyword(name);
        } catch (Exception e)
        {
            return null;
        }
    }

    public void update(VOKeyword vo)throws Exception
    {
        kc = getInstance();
        Keyword k = kc.findKeyword(vo.getIdKeyword());
        try
        {
            kc.edit(k);
        } catch (Exception e)
        {
            if(e instanceof NonexistentEntityException)
            {
                throw new NonExistentObjectException();
            }
            throw new DatabaseException("KeywordService.update Operation");
        }
    }

    private void updateObject(Keyword k)throws Exception
    {
        try
        {
            kc.edit(k);
        } catch (Exception e)
        {
            if(e instanceof NonexistentEntityException)
            {
                throw new NonExistentObjectException();
            }
            throw new DatabaseException("KeywordService.update Operation");
        }
    }

    public void delete(VOKeyword vo)throws Exception
    {
        kc = getInstance();
        try
        {
            kc.destroy(vo.getIdKeyword());
        } catch (Exception e)
        {
            if( e instanceof NonexistentEntityException)
            {
                throw new NonExistentObjectException();
            }
            throw new DatabaseException("KeywordService.delete Operation");
        }
    }

    public ArrayList<VOKeyword> findAll()
    {
        kc =getInstance();
        List<Keyword> l = kc.findKeywordEntities();
        if(l == null || l.isEmpty())
            return null;
        ArrayList<VOKeyword> ret = new ArrayList<VOKeyword>();
        for(Keyword k : l)
            ret.add(k.getVO());
        return ret;
    }

    public void addToProblem(String keyword  , String problemcode) throws Exception
    {
        ps = getProblemService();
        try
        {
            Keyword k = findObject(keyword);
            if(k == null)
                throw new NonExistentObjectException("Keyword");
            Problem p = ps.findObject(problemcode);
            if(p == null)
                throw new NonExistentObjectException("Problem");

            k.getProblemList().add(p);
            p.getKeywordList().add(k);
            updateObject(k);
            ps.updateObject(p);
        }
        catch (Exception e)
        {
            throw e;
        }
    }


}
